package org.codeart.juc.util;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 循环栅栏
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier barrier = new CyclicBarrier(3, () -> {
            System.out.println("等到各位都到位之后，分发护照和签证。");
        });
        new Thread(() -> {
            System.out.println("Tom到位！");
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                System.out.println("人没到齐！");
            }
            System.out.println("Tom出发！");
        }).start();
        
        Thread.sleep(100);
        new Thread(() -> {
            System.out.println("Jack到位！");
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                System.out.println("人没到齐！");
            }
            System.out.println("Jack出发！");
        }).start();

        Thread.sleep(100);
        new Thread(() -> {
            System.out.println("Rose到位！");
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                System.out.println("人没到齐！");
            }
            System.out.println("Rose出发！");
        }).start();
        
    }

}
